/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

/**
 *
 * @author KLEBER
 */
public class Geral {

    public static boolean StrEmpty(String Str) {
        return Str == null || Str.trim().equals("");
    }

    public static String TrocaChars(String Str, String[] CharOld, String CharNew) {
        if (StrEmpty(Str)) {
            return "";
        }
        //
        int i, j, size = CharOld.length;
        StringBuilder Result = new StringBuilder("");
        for (i = 0; i < Str.length(); i++) {
            for (j = 0; j < size; j++) {
                if (String.valueOf(Str.charAt(i)).equals(CharOld[j])) {
                    Result.append(CharNew);
                    break;
                }
            }
            if (j == size) {
                Result.append(Str.charAt(i));
            }
        }
        return Result.toString();
    }

    public static String TirarAcentos(String Str) {
        return TirarAcentos(Str, true);
    }

    public static String TirarAcentos(String Str, boolean clearIfEmpty) {
        if (clearIfEmpty && StrEmpty(Str)) {
            return "";
        }
        int i;
        StringBuilder Result = new StringBuilder("");
        for (i = 0; i < Str.length(); i++) {
            switch (Str.charAt(i)) {
                case 'Á':
                case 'Ä':
                case 'À':
                case 'Â':
                case 'Ã':
                    Result.append('A');
                    break;
                case 'á':
                case 'ä':
                case 'à':
                case 'â':
                case 'ã':
                    Result.append('a');
                    break;
                case 'É':
                case 'Ë':
                case 'È':
                case 'Ê':
                    //case '?':
                    Result.append('E');
                    break;
                case 'é':
                case 'ë':
                case 'è':
                case 'ê':
                    //case '?':
                    Result.append('e');
                    break;
                case 'Í':
                case 'Ï':
                case 'Ì':
                case 'Î':
                    //case '?':
                    Result.append('I');
                    break;
                case 'í':
                case 'ï':
                case 'ì':
                case 'î':
                    //case '?':
                    Result.append('i');
                    break;
                case 'Ó':
                case 'Ö':
                case 'Ò':
                case 'Ô':
                case 'Õ':
                    Result.append('O');
                    break;
                case 'ó':
                case 'ö':
                case 'ò':
                case 'ô':
                case 'õ':
                    Result.append('o');
                    break;
                case 'Ú':
                case 'Ü':
                case 'Ù':
                case 'Û':
                    //case '?':
                    Result.append('U');
                    break;
                case 'ú':
                case 'ü':
                case 'ù':
                case 'û':
                    //case '?':
                    Result.append('u');
                    break;
                case 'Ý':
                case '?':
                    //case '?':
                    //case '?':
                    //case '?':
                    Result.append('Y');
                    break;
                case 'ý':
                case 'ÿ':
                    //case '?':
                    //case '?':
                    //case '?':
                    Result.append('y');
                    break;
                case 'Ç':
                    Result.append('C');
                    break;
                case 'ç':
                    Result.append('c');
                    break;
                case '©':
                    Result.append("c");
                    break;
                case '§':
                    Result.append("s");
                    break;
                case 'ª':
                    Result.append("a");
                    break;
                case 'º':
                    Result.append("o");
                    break;
                case '°':
                    Result.append("o");
                    break;
                case 'Ñ':
                    Result.append('N');
                    break;
                case 'ñ':
                    Result.append('n');
                    break;
                case '&':
                    Result.append('E');
                    break;
                default:
                    Result.append(Str.charAt(i));
            }
        }
        return Result.toString();
    }

    public static String TirarOutrosASCII(String Str) {
        return TirarOutrosASCII(Str, true);
    }

    public static String TirarOutrosASCII(String Str, boolean clearIfEmpty) {
        if (clearIfEmpty && StrEmpty(Str)) {
            return "";
        }
        int i;
        StringBuilder Result = new StringBuilder("");
        for (i = 0; i < Str.length(); i++) {
            if ((Str.charAt(i) >= '0' && Str.charAt(i) <= '9')
                    || (Str.charAt(i) >= 'A' && Str.charAt(i) <= 'Z')
                    || (Str.charAt(i) >= 'a' && Str.charAt(i) <= 'z')) {
                Result.append(Str.charAt(i));
            }
        }
        return Result.toString();
    }

    public static String TirarOutrosASCII2(String Str) {
        return TirarOutrosASCII2(Str, true);
    }

    public static String TirarOutrosASCII2(String Str, boolean clearIfEmpty) {
        if (clearIfEmpty && StrEmpty(Str)) {
            return "";
        }
        int i;
        StringBuilder Result = new StringBuilder("");
        for (i = 0; i < Str.length(); i++) {
            if ((Str.charAt(i) >= '0' && Str.charAt(i) <= '9')
                    || (Str.charAt(i) >= 'A' && Str.charAt(i) <= 'Z')
                    || (Str.charAt(i) >= 'a' && Str.charAt(i) <= 'z')
                    || (Str.charAt(i) == '.')) {
                Result.append(Str.charAt(i));
            }
        }
        return Result.toString();
    }

    public static String TirarZeroAEsq(String s) {
        return TirarCharAEsq(s, "0");
    }

    public static String TirarCharAEsq(String s, String c) {
        while (s.length() > 0 && s.startsWith(c)) {
            s = s.substring(1);
        }
        return s;
    }
}
